package com.car.controller;

import com.car.pojo.MaintenanceAppointment;
import com.car.pojo.MaintenanceAppointmentQueryParam;
import com.car.pojo.Result;
import com.car.service.MaintenanceAppointmentService;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 保养预约管理Controller
 */
@Slf4j
@RequestMapping("/appointments")
@RestController
public class MaintenanceAppointmentController {

    @Autowired
    private MaintenanceAppointmentService maintenanceAppointmentService;
    

    /**
     * 分页查询保养预约
     */
    @GetMapping("/maintenance")
    public Result page(MaintenanceAppointmentQueryParam param) {
        log.info("分页查询保养预约: {}", param);
        return Result.success(maintenanceAppointmentService.page(param));
    }

    /**
     * 根据ID查询保养预约详情
     */
    @GetMapping("/maintenance/{id}")
    public Result getInfo(@PathVariable Long id) {
        log.info("根据ID查询保养预约详情: {}", id);
        MaintenanceAppointment appointment = maintenanceAppointmentService.getInfo(id);
        return Result.success(appointment);
    }

    /**
     * 更新保养预约状态和分配保养员
     */
    @PutMapping("/maintenance/status")
    public Result updateStatus(@RequestParam Long id, 
                              @RequestParam String status,
                              @RequestParam(required = false) String empName) {
        log.info("更新保养预约状态: id={}, status={}, 分配保养员: {}", id, status, empName);
        maintenanceAppointmentService.updateStatus(id, status, empName);
        return Result.success();
    }

    /**
     * 批量删除保养预约
     */
    @DeleteMapping("/maintenance")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除保养预约: {}", ids);
        maintenanceAppointmentService.delete(ids);
        return Result.success();
    }

    /**
     * 查询所有保养预约（用于导出等）
     */
    @GetMapping("/maintenance/list")
    public Result list(MaintenanceAppointmentQueryParam param) {
        log.info("查询所有保养预约数据: {}", param);
        List<MaintenanceAppointment> list = maintenanceAppointmentService.list(param);
        return Result.success(list);
    }
    
    /**
     * 添加保养预约（用户提交预约）
     */
    @PostMapping("/maintenance")
    public Result add(@RequestBody MaintenanceAppointment appointment) {
        log.info("添加保养预约: {}", appointment);
        maintenanceAppointmentService.add(appointment);
        return Result.success();
    }
    
    /**
     * 根据用户ID查询保养预约
     */
    @GetMapping("/maintenance/user")
    public Result listByUserId(@RequestParam Long userId) {
        log.info("根据用户ID查询保养预约: userId={}", userId);
        List<MaintenanceAppointment> appointments = maintenanceAppointmentService.listByUserId(userId);
        return Result.success(appointments);
    }
    
    /**
     * 根据用户ID统计保养预约数量
     */
    @GetMapping("/maintenance/user/count")
    public Result countByUserId(@RequestParam Long userId) {
        log.info("根据用户ID统计保养预约数量: userId={}", userId);
        Integer count = maintenanceAppointmentService.countByUserId(userId);
        return Result.success(count);
    }

    
    /**
     * 根据员工名称参数查询保养预约
     */
    @GetMapping("/maintenance/by-emp-name")
    public Result listByEmpNameParam(@RequestParam String empName) {
        log.info("根据员工名称参数查询保养预约: empName={}", empName);
        List<MaintenanceAppointment> appointments = maintenanceAppointmentService.listByEmpName(empName);
        return Result.success(appointments);
    }
    
    /**
     * 获取空闲保养员列表
     * 查询job_id=3且status='空闲'的员工
     */
    @GetMapping("/maintenance/maintenance-staff")
    public Result getMaintenanceStaff() {
        log.info("获取空闲保养员列表");
        List<Map<String, Object>> staffList = maintenanceAppointmentService.getMaintenanceStaff();
        return Result.success(staffList);
    }
    
    /**
     * 统计当前登录员工所关联的状态为'已确认'的保养预约数量
     */
    @GetMapping("/maintenance/confirmed-count")
    public Result countConfirmedByEmpName(@RequestParam String empName) {
        log.info("统计当前员工已确认保养预约数量: empName={}", empName);
        Integer count = maintenanceAppointmentService.countConfirmedByEmpName(empName);
        return Result.success(count);
    }
    
    /**
     * 统计当前登录员工所关联的状态为'已完成'的保养预约数量
     */
    @GetMapping("/maintenance/completed-count")
    public Result countCompletedByEmpName(@RequestParam String empName) {
        log.info("统计当前员工已完成保养预约数量: empName={}", empName);
        Integer count = maintenanceAppointmentService.countCompletedByEmpName(empName);
        return Result.success(count);
    }
    


}